import * as actionTypes from "./constants";

import {
  getDjRadioCatelist,
  getDjRadioRecommend,
  getDjRadios,
} from "@/services/djradio";

const changeCategoriesAction = (res) => ({
  type: actionTypes.CHANGE_CATEGORIES,
  categories: res.categories,
});

const changeRecommendsAction = (res) => ({
  type: actionTypes.CHANGE_RECOMMENDS,
  recommends: res.djRadios,
});

export const changeCurrentIdAction = (id) => ({
  type: actionTypes.CHANGE_CURRENT_ID,
  currentId: id,
});

const changeRadiosAction = (res) => ({
  type: actionTypes.CHANGE_RADIOS,
  radios: res.djRadios,
});

export const getDjRadioCatelistAction = () => {
  return (dispatch) => {
    getDjRadioCatelist().then((res) => {
      dispatch(changeCategoriesAction(res));
      const currentId = res.categories[0].id;
      dispatch(changeCurrentIdAction(currentId));
    });
  };
};

export const getDjRadioRecommendAction = (currentId) => {
  return (dispatch) => {
    getDjRadioRecommend(currentId).then((res) => {
      dispatch(changeRecommendsAction(res));
    });
  };
};

export const getDjRadiosAction = (currentId, offset) => {
  return (dispatch) => {
    getDjRadios(currentId, 30, offset).then((res) => {
      dispatch(changeRadiosAction(res));
    });
  };
};
